package com.such.kit.bean.bean;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import com.such.kit.bean.BeanCommons;
import com.such.kit.validate.ValidateCommons;

/**
 * @author SUCH
 * <pre>
 * 对象比较结果
 * 使用 {@link BeanCommons#compare(Object, Object, List)} 方法比较 2 个对象后产生的结果
 * </pre>
 */
public class CompareResult<T> {

	/** SAME_YES：是同 1 条数据 */
	public static final String SAME_YES = "YES";
	/** SAME_NO：不是同 1 条数据 */
	public static final String SAME_NO = "NO";
	/** SAME_UNKNOWN：未知，一般是因为未指定 {@link #keyAttributes} 导致的 */
	public static final String SAME_UNKNOWN = "UNKNOWN";

	/** bean1：比较对象 1 */
	private T bean1;
	/** bean2：比较对象 2 */
	private T bean2;
	private Map<String, Object> beanMap1;
	private Map<String, Object> beanMap2;
	/** attributes：属性集 */
	private List<String> attributes;
	/** keyAttributes：关键字段，原则上可以用于区分 bean 的唯一性，既二个 bean 从业务上是否是一条数据 */
	private List<String> keyAttributes;
	/** same：是否是一条数据 [参考 SAME_*] */
	private String same;
	/** differences：不同的字段集 */
	private List<String> differences;

	/**
	 * @param bean1 比较对象 1
	 * @param bean2 比较对象 2
	 * @param keyAttributes [参考 {@link #keyAttributes}]
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public CompareResult(T bean1, T bean2, List<String> keyAttributes) 
			throws SecurityException, IllegalArgumentException, NoSuchMethodException, 
			IllegalAccessException, InvocationTargetException {
		this.bean1 = bean1;
		this.bean2 = bean2;
		this.beanMap1 = BeanCommons.toMap(this.bean1);
		this.beanMap2 = BeanCommons.toMap(this.bean2);
		this.attributes = BeanCommons.getAttributeNames(this.bean1.getClass());
	}

	/**
	 * <pre>
	 * 从比较对象 1 中获取指定属性的值
	 * 未找到指定属性则返回空字符串
	 * </pre>
	 * @param attribute 属性
	 * @return 属性的值
	 */
	public Object getBean1Value(String attribute) {
		Object value1 = this.beanMap1.get(attribute);
		if (ValidateCommons.isEmpty(value1)) {
			value1 = "";
			this.beanMap1.put(attribute, value1);
		}
		return value1;
	}

	/**
	 * <pre>
	 * 从比较对象 2 中获取指定属性的值
	 * 未找到指定属性则返回空字符串
	 * </pre>
	 * @param attribute 属性
	 * @return 属性的值
	 */
	public Object getBean2Value(String attribute) {
		Object value2 = this.beanMap2.get(attribute);
		if (ValidateCommons.isEmpty(value2)) {
			value2 = "";
			this.beanMap2.put(attribute, value2);
		}
		return value2;
	}

	/** [参考 {@link #attributes}] */
	public List<String> getAttributes() {
		return this.attributes;
	}

	/** [参考 {@link #keyAttributes}] */
	public List<String> getKeyAttributes() {
		return this.keyAttributes;
	}

	/** [参考 {@link #same}] */
	public String getSame() {
		return this.same;
	}

	/** [参考 {@link #differences}] */
	public List<String> getDifferences() {
		return this.differences;
	}

	/**
	 * <pre>
	 * 填充比较结果
	 * </pre>
	 * @param same [参考 {@link #same}]
	 * @param differences [参考 {@link #differences}]
	 */
	public void setResult(String same, List<String> differences) {
		this.same = same;
		this.differences = differences;
	}

}
